﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HWR_Ants.Classes;
using HWR_Ants.Tools;


namespace HWR_Ants.Forms
{
    public partial class fOptions : Form
    {
        private static string optionsFile = "options.xml";
        private static string defaultFile = "defaultOptions.xml";

        public fOptions()
        {
            InitializeComponent();
            fillTextboxes(loadOptions());
        }

        private Options loadOptions()
        {
            return loadOptions(optionsFile);
        }

        private Options loadOptions(string Filename)
        {
            try
            {
                Options o;
                if (File.Exists(Filename))
                {
                    o = Tools.Tools.readXML<Options>(Filename);
                }
                else
                {
                    o = new Options();
                    Tools.Tools.writeXML<Options>(o, Filename);
                }
                return o;
            }
            catch
            {
                MessageBox.Show("Es ist ein Fehler beim laden der Optionen aufgetreten.", "Error");
            }
            return new Options();
        }

        private void fillTextboxes(Options o)
        {
            tb_AntsCount.Text = o.AntsCounter.ToString();
            tb_CityCount.Text = o.CityCounter.ToString();
            tb_IterationCount.Text = o.IterationCounter.ToString();
            tb_Stopp.Text = o.Stopp.ToString();

            tb_Param_Phero.Text = o.Parameter.Alpha.ToString();
            tb_Param_Locale.Text = o.Parameter.Beta.ToString();
            tb_Param_Verdunstung.Text = o.Parameter.Rho.ToString();
            tb_Param_Initial_Pheromon.Text = o.Parameter.Tau.ToString();
            tb_Param_Pheromon_Update.Text = o.Parameter.Q.ToString();
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                Options o = new Options();
                o.AntsCounter = Convert.ToInt32(tb_AntsCount.Text);
                o.CityCounter = Convert.ToInt32(tb_CityCount.Text);
                o.IterationCounter = Convert.ToInt32(tb_IterationCount.Text);
                o.Parameter.Alpha = Convert.ToInt32(tb_Param_Phero.Text);
                o.Parameter.Beta= Convert.ToInt32(tb_Param_Locale.Text);
                o.Parameter.Rho= Convert.ToDouble(tb_Param_Verdunstung.Text);
                o.Parameter.Tau = Convert.ToInt32(tb_Param_Initial_Pheromon.Text);
                o.Parameter.Q= Convert.ToInt32(tb_Param_Pheromon_Update.Text);
                o.Stopp = tb_Stopp.Text;
                saveOptions(optionsFile, o);
                
                this.Close();
                Tools.Tools.ActiveCurrentOptions = Tools.Tools.getCurrentStoredOptions();
            }
            catch
            {
                fillTextboxes(loadOptions(defaultFile));
                MessageBox.Show("Es ist ein Fehler aufgetreten. Defaults geladen.", "Error");
            }
        }

        private void bt_LoadDefaults_Click(object sender, EventArgs e)
        {
            Options o = loadOptions(defaultFile);
            fillTextboxes(o);
            // saveOptions(optionsFile, o);
        }

        private void saveOptions(string filename, Options o)
        {
            Tools.Tools.writeXML<Options>(o, filename);
        }
    }
}
